package com.gexiaobao.pigeon.ui.fragment.mine.pigeon

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.model.bean.PigeonBean
import com.gexiaobao.pigeon.app.model.bean.PigeonFosterBean
import com.gexiaobao.pigeon.databinding.FragmentPigeonDetailBinding
import com.gexiaobao.pigeon.ui.adapter.MyPagerAdapter
import com.gexiaobao.pigeon.viewmodel.MyPigeonViewModel
import me.hgj.jetpackmvvm.ext.nav

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 16:45
 * @Describe    : 赛鸽详情
 */
class FragmentPigeonDetail : BaseFragment<MyPigeonViewModel, FragmentPigeonDetailBinding>() {

    private var pigeonID: String = ""
    private var mType: String = ""
    private var intentType: Boolean = false
    private lateinit var pigeonData: PigeonBean.RingInfo
    private lateinit var fosterPigeonData: PigeonFosterBean.ListData

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.includeBar.toolbar.initClose(getString(R.string.pigeon_detail)) {
            nav().navigateUp()
        }
        pigeonID = arguments?.getString("pigeonId").toString()
        mType = arguments?.getString("type").toString()
        intentType = requireArguments().getBoolean("intentType")
        if (mType == "foster") {
            fosterPigeonData = arguments?.getSerializable("pigeonData") as PigeonFosterBean.ListData
        } else {
            pigeonData = arguments?.getSerializable("pigeonData") as PigeonBean.RingInfo
        }
        initViewPager(pigeonID)
    }

    private fun initViewPager(pigeonID: String) {
        val tabTitles = arrayListOf(getString(R.string.base_info), getString(R.string.Training_record))
        val fragmentList = ArrayList<Fragment>()
        if (mType != "foster") {
            fragmentList.add(FragmentPigeonBasicInfo.newInstance(pigeonID, pigeonData, intentType))
        } else {
            fragmentList.add(FragmentFosterPigeonBasicInfo.newInstance(pigeonID, fosterPigeonData))
        }
        fragmentList.add(FragmentTrainingRecord.newInstance(pigeonID))
        mDatabind.viewPager.adapter =
            context?.let { MyPagerAdapter(childFragmentManager, it, fragmentList, tabTitles) }
        mDatabind.viewPager.offscreenPageLimit = 2
        mDatabind.tabLayout.setupWithViewPager(mDatabind.viewPager)
    }
}